angular.module("crawlerApp").controller("loggerEchartsCtrl", function ($scope, $http, $state, $stateParams) {
  /*      $scope.rate={
            value:""
        };*/
    $scope.rate='1';
    $scope.chart = true;
    $scope.config = $stateParams.config;
    initLineSize();

    var lineChart = echarts.init(document.getElementById("lineChart"));

    $('.form_datetime').datetimepicker({
        /*  language:  'zh-CN',*/
        weekStart: 1,
        todayBtn: 1,
        autoclose: 1,
        todayHighlight: 1,
        startView: 2,
        forceParse: 0,
        showMeridian: 1
    });


    var param = {
        "query": {
            "bool": {
                "must": [{
                    "match": {
                        "taskid": $scope.config.id
                    }
                }]
            }
        },
        "aggs": {
            "articles_over_time": {
                "date_histogram": {
                    "field": "@timestamp",
                    "interval": "hour"
                }
            }
        }
    };
    analysisLog(param);

    function analysisLog(param) {
        $scope.timeList = [];
        $scope.countList = [];
        $http.post("log/es/getLoggerBy", param).then(function (response) {
            $scope.loggers = response.data;
            angular.forEach($scope.loggers, function (data) {
                $scope.timeList.push(data.time);
                $scope.countList.push(data.count);
            });
            drawLine();
        });


    }

    function initLineSize() {
        var windowWidth =  window.screen.width;
        var windowHeight =  window.screen.height;
        if (windowWidth > 1366) {
            $("#lineChart").css({"width": (windowWidth - 140 * 2) + "px", "height": 600 + "px"});
        } else {
            $("#lineChart").css({"width": 1000 + "px", "height": 400 + "px"});
        }
        if (windowHeight > 768) {
            $("#lineChart").css({"width": (windowWidth - 140 * 2) + "px", "height": 600 + "px"});
        } else {
            $("#lineChart").css({"width": (windowWidth - 140 * 2) + "px", "height": 480 + "px"});
        }
    }


    function drawLine() {
        var option = {
            title: {
                text: '日志记录情况',
            },
            tooltip: {
                trigger: 'axis'
            },

            calculable: true,
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    data: $scope.timeList
                }
            ],

            yAxis: {
                type: 'value'
            },
            series: [{
                data: $scope.countList,
                type: 'line'
            }]

        };

        lineChart.setOption(option);
    }


    $scope.submit = function () {
        if ($scope.rate == 1) {
            var param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        },
                            {
                                "range": {
                                    "@timestamp": {
                                        "gt": new Date($scope.start_time).getTime(),
                                        "lt": new Date($scope.end_time).getTime()
                                    }
                                }
                            }]
                    }
                },
                "aggs": {
                    "articles_over_time": {
                        "date_histogram": {
                            "field": "@timestamp",
                            "interval": "hour"
                        }
                    }
                }
            };
        } else if ($scope.rate == 2) {
            var param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date($scope.start_time).getTime(),
                                    "lt": new Date($scope.end_time).getTime()
                                }
                            }
                        }]
                    }
                },
                "aggs": {
                    "articles_over_time": {
                        "date_histogram": {
                            "field": "@timestamp",
                            "interval": "day"
                        }
                    }
                }
            };
        } else if ($scope.rate == 3) {
            var param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date($scope.start_time).getTime(),
                                    "lt": new Date($scope.end_time).getTime()
                                }
                            }
                        }]
                    }
                },
                "aggs": {
                    "articles_over_time": {
                        "date_histogram": {
                            "field": "@timestamp",
                            "interval": "week"
                        }
                    }
                }
            };
        } else if ($scope.rate == 4) {
            var param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date($scope.start_time).getTime(),
                                    "lt": new Date($scope.end_time).getTime()
                                }
                            }
                        }]
                    }
                },
                "aggs": {
                    "articles_over_time": {
                        "date_histogram": {
                            "field": "@timestamp",
                            "interval": "month"
                        }
                    }
                }
            };
        } else if ($scope.rate == 5) {
            var param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date($scope.start_time).getTime(),
                                    "lt": new Date($scope.end_time).getTime()
                                }
                            }
                        }]
                    }
                },
                "aggs": {
                    "articles_over_time": {
                        "date_histogram": {
                            "field": "@timestamp",
                            "interval": "year"
                        }
                    }
                }
            };
        } else {

        }
        analysisLog(param);
    };

    $scope.goBack = function () {
        $state.go("logger",{config:$scope.config });
    };
    $scope.pageNum = 0;
    lineChart.on('click', function (para) {
        $scope.chart = false;

        if ($scope.rate == 1) {
            $scope.param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date(para.name).getTime(),
                                    "lt": new Date(para.name).getTime() + 3600000
                                }
                            }
                        }]
                    }
                },
                "size": 10,
                "from": 10 * $scope.pageNum,
                "sort": [
                    {
                        "@timestamp": {"order": "desc"}
                    }
                ]
            };
        } else if ($scope.rate == 2) {
            $scope.param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date(para.name).getTime(),
                                    "lt": new Date(para.name).getTime() + 3600000 * 24
                                }
                            }
                        }]
                    }
                },
                "size": 10,
                "from": 10 * $scope.pageNum,
                "sort": [
                    {
                        "@timestamp": {"order": "desc"}
                    }
                ]
            };
        } else if ($scope.rate == 3) {
            $scope.param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date(para.name).getTime(),
                                    "lt": new Date(para.name).getTime() + 3600000 * 24 * 7
                                }
                            }
                        }]
                    }
                },
                "size": 10,
                "from": 10 * $scope.pageNum,
                "sort": [
                    {
                        "@timestamp": {"order": "desc"}
                    }
                ]
            };
        } else if ($scope.rate == 4) {
            $scope.param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date(para.name).getTime(),
                                    "lt": new Date(getNextMonth(para.name)).getTime()
                                }
                            }
                        }]
                    }
                },
                "size": 10,
                "from": 10 * $scope.pageNum,
                "sort": [
                    {
                        "@timestamp": {"order": "desc"}
                    }
                ]
            };
        } else if ($scope.rate == 5) {
            var year = new Date(para.name).getFullYear();
            $scope.param = {
                "query": {
                    "bool": {
                        "must": [{
                            "match": {
                                "taskid": $scope.config.id
                            }
                        }, {
                            "range": {
                                "@timestamp": {
                                    "gt": new Date(para.name).getTime(),
                                    "lt": new Date(year + 1).getTime()
                                }
                            }
                        }]
                    }
                },
                "size": 10,
                "from": 10 * $scope.pageNum,
                "sort": [
                    {
                        "@timestamp": {"order": "desc"}
                    }
                ]
            };
        } else {

        }
        getLogger($scope.param);


    });

    function getLogger(param) {

        $http.post("log/es/getLogger", param).then(function (response) {
            $scope.loggers = response.data.data;
            $scope.totalCount = response.data.total;
            if (Number(response.data.total) % 10 == 0) {
                $scope.totalPage = Number(response.data.total) / 10;
            } else {
                $scope.totalPage = Math.ceil(response.data.total / 10);
            }

        });

    };
    $scope.Previous = function (pageNum) {
        if (pageNum - 1 >= 0) {
            $scope.pageNum = pageNum - 1;
        }
        $scope.param.from = $scope.pageNum * 10;
        getLogger($scope.param);
    };

    $scope.Next = function (page) {
        if (page + 1 <= $scope.totalPage - 1) {
            $scope.pageNum = page + 1;
        }
        $scope.param.from = $scope.pageNum * 10;
        getLogger($scope.param);
    };

    $scope.goto = function (page) {
        if (Number(page) <= $scope.totalPage && Number(page) > 0) {
            $scope.pageNum = Number(page) - 1;
        }
        $scope.param.from = $scope.pageNum * 10;
        getLogger($scope.param);
    };

    function getNextMonth(date) {
        var arr = date.split('-');
        var year = arr[0]; //获取当前日期的年份
        var month = arr[1]; //获取当前日期的月份
        var day = arr[2]; //获取当前日期的日
        var days = new Date(year, month, 0);
        days = days.getDate(); //获取当前日期中的月的天数
        var year2 = year;
        var month2 = parseInt(month) + 1;
        if (month2 == 13) {
            year2 = parseInt(year2) + 1;
            month2 = 1;
        }
        var day2 = day;
        var days2 = new Date(year2, month2, 0);
        days2 = days2.getDate();
        if (day2 > days2) {
            day2 = days2;
        }
        if (month2 < 10) {
            month2 = '0' + month2;
        }

        var t2 = year2 + '-' + month2 + '-' + day2;
        return t2;
    }

    $scope.return = function () {
        $scope.chart = true;
    }

});